package nl.hanze.designpatterns.mvc.view;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import nl.hanze.designpatterns.DAOFactory.DAOFactory;
import nl.hanze.designpatterns.DAOFactory.impl.DAOFactoryDB;
import nl.hanze.designpatterns.DAOFactory.impl.DAOFactoryFile;
import nl.hanze.designpatterns.mvc.controller.LoginController;
import nl.hanze.designpatterns.mvc.model.LoginModel;

public class LoginFormView extends View<LoginController, LoginModel> {

	private JTextField usernameField;
	private JPasswordField passwordField;
	private JButton loginButton;
	private JComboBox<DAOFactory> factoryComboBox;
	
	public LoginFormView(LoginController controller, LoginModel model) {
		super(controller, model);
		
		usernameField = new JTextField();
		passwordField = new JPasswordField();
		loginButton = new JButton("Login");
		loginButton.setActionCommand("login");
		loginButton.putClientProperty("username", usernameField);
		loginButton.putClientProperty("password", passwordField);
		loginButton.addActionListener(controller);
		factoryComboBox = new JComboBox<DAOFactory>(new DAOFactory[]{null, new DAOFactoryDB(), new DAOFactoryFile()});
		factoryComboBox.addActionListener(controller);
		factoryComboBox.setActionCommand("select_factory");
		
		FormLayout layout = new FormLayout(
				"right:pref, 5dlu, 50dlu, 5dlu, pref",
				"p, 5dlu, p, 5dlu, p, 5dlu, p");
		
		PanelBuilder builder = new PanelBuilder(layout);
		// Obtain a reusable constraints object to place components in the grid.
		CellConstraints cc = new CellConstraints();

		// Add a content to the form
		builder.addLabel("Username",    cc.xy (1,  1));
		builder.addLabel("Password",    cc.xy (1,  3));
		builder.add(usernameField, 		cc.xyw(3, 1, 3));
		builder.add(passwordField, 		cc.xyw(3, 3, 3));
		builder.addLabel("Factory", cc.xyw(1, 5, 1));
		builder.add(factoryComboBox, cc.xyw(3, 5, 3));
		builder.add(loginButton, 		cc.xy(5, 7));
		
		this.add(builder.getPanel());
	}
	
	public String getUsername(){
		return this.usernameField.getText();
	}
	
	public String getPassword(){
		return this.passwordField.getPassword().toString();
	}

	@Override
	public void notifyChange() {
		
	}

	
}
